﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyDuAn.BUS;
using QuanLyDuAn.Report;
using System.Collections;

namespace QuanLyDuAn.GUI
{
    public partial class FrmSearchVacancy : DevComponents.DotNetBar.Office2007Form
    {
        VacancyController vacancy = new VacancyController();
        DataSet dsStatus = new DataSet();
        DataSet dsTitle = new DataSet();
        DataSet dsDate = new DataSet();
        DataSet dsDepartment = new DataSet();
        ArrayList list = new ArrayList();
        public FrmSearchVacancy()
        {
            InitializeComponent();
        }

        private void FrmSearchVacancy_Load(object sender, EventArgs e)
        {
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            cmbCriterion.Items.Add("Status");
            cmbCriterion.Items.Add("Date Close");
            cmbCriterion.Items.Add("Department");
            cmbCriterion.Items.Add("Title");
            cmbCriterion.SelectedItem = "--Chose--";
            cmbDepartment.Items.Add("Software I");
            cmbDepartment.Items.Add("Software II");
            cmbDepartment.Items.Add("Software III");
            cmbDepartment.SelectedItem = "Software I";
            dtInput.Enabled = false;
            rdCLose.Enabled = false;
            rdOpen.Enabled = false;
            rdWait.Enabled = false;
            cmbDepartment.Enabled = false;
            txtTitle.Enabled = false;
            this.LoadDataToCollection();
            
        }
        public void CriterionChange()
        {
            String criterion = cmbCriterion.SelectedItem.ToString();
            if (criterion.CompareTo("Status") == 0)
            {
                MessageBox.Show("Please select status !");
                dtInput.Enabled = false;
                rdCLose.Enabled = true;
                rdOpen.Enabled = true;
                rdWait.Enabled = true;
                cmbDepartment.Enabled = false;
                txtTitle.Enabled = false;
            }
            else if (criterion.CompareTo("Date Close") == 0)
            {
                MessageBox.Show("Please select date close !");
                dtInput.Enabled = true;
                rdCLose.Enabled = false;
                rdOpen.Enabled = false;
                rdWait.Enabled = false;
                cmbDepartment.Enabled = false;
                txtTitle.Enabled = false;
            }
            else if (criterion.CompareTo("Department") == 0)
            {
                MessageBox.Show("Please select department !");
                dtInput.Enabled = false;
                rdCLose.Enabled = false;
                rdOpen.Enabled = false;
                rdWait.Enabled = false;
                cmbDepartment.Enabled = true;
                txtTitle.Enabled = false;
            }
            else
            {
                MessageBox.Show("Please enter title of vacancy !");
                dtInput.Enabled = false;
                rdCLose.Enabled = false;
                rdOpen.Enabled = false;
                rdWait.Enabled = false;
                cmbDepartment.Enabled = false;
                txtTitle.Enabled = true;
            }            
        }
        public int ValueCriterrion()
        {
            int selection;
            String criter = cmbCriterion.Text;
            if (criter.CompareTo("") != 0)
            {
                String criterion = cmbCriterion.SelectedItem.ToString();
                if (criterion.CompareTo("Status") == 0)
                {
                    selection = 1;
                }
                else if (criterion.CompareTo("Date Close") == 0)
                {
                    selection = 2;
                }
                else if (criterion.CompareTo("Department") == 0)
                {
                    selection = 3;
                }
                else
                {
                    selection = 4;
                }
                return selection;
            }
            else
            {
                selection = 0;
                MessageBox.Show("Please select Criterion !");
            }
            return selection;
        }
        public String GetStatus()
        {
            String status;
            if (rdCLose.Checked == true)
            {
                status = "Close";
            }
            else if (rdOpen.Checked == true)
            {
                status = "Open";
            }
            else
            {
                status = "Wait";
            }
            return status;
        }
        public void SearchByStatus()
        {
            String status = this.GetStatus();
            dsStatus = vacancy.SearchVacancyByStatus(status);
            dataGridViewX1.DataSource = dsStatus.Tables[0];
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; 
        }

        public void LoadDataToCollection()
        {
            AutoCompleteStringCollection autoString = new AutoCompleteStringCollection();
            list = vacancy.GetTitleVacancy();
            foreach (String title in list)
            {
                autoString.Add(title);
            }
            txtTitle.AutoCompleteMode = AutoCompleteMode.Append;
            txtTitle.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtTitle.AutoCompleteCustomSource = autoString;
        }

        public void SearchByTitle()
        {
            String title = txtTitle.Text;
            dsTitle = vacancy.SearchVacancyByTitile(title);
            dataGridViewX1.DataSource = dsTitle.Tables[0];
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; 
        }
        public void SearchByDate()
        {
            String date = dtInput.Value.ToString();
            dsDate = vacancy.SearchVacancyByDate(date);
            dataGridViewX1.DataSource = dsDate.Tables[0];
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; 
        }
        public void SearchByDepartment()
        {
            String department = cmbDepartment.SelectedItem.ToString();
            dsDepartment = vacancy.SearchVacancyByDepartment(department);
            dataGridViewX1.DataSource = dsDepartment.Tables[0];
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; 
        }
        private void cmbCriterion_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CriterionChange();
        }


        private void btnSearch_Click(object sender, EventArgs e)
        {
            int i = this.ValueCriterrion();
            if(i ==1)
            {
                this.SearchByStatus();
            }
            else if (i == 4)
            {
                this.SearchByTitle();
            }
            if (i == 2)
            {
                this.SearchByDate();
            }
            else if (i == 3)
            {
                this.SearchByDepartment();
            }
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            int i = this.ValueCriterrion();
            if (i == 1)
            {
                String status = GetStatus();
                FrmVacancySatatus frmStatus = new FrmVacancySatatus();
                frmStatus.value = status;
                frmStatus.Visible = true;
                frmStatus.Show();
            }
            else if (i == 4)
            {
                String title = txtTitle.Text;
                FrmVacancyTitle frmTitle= new FrmVacancyTitle();
                frmTitle.value = title;
                frmTitle.Visible = true;
                frmTitle.Show();
            }
            if (i == 2)
            {
                DateTime date = dtInput.Value;
                FrmVacancyDate frmDate = new FrmVacancyDate();
                frmDate.value = date;
                frmDate.Visible = true;
                frmDate.Show();
            }
            else if (i == 3)
            {
                String department = cmbDepartment.SelectedItem.ToString();
                FrmVacancyDepartment frmDepartment = new FrmVacancyDepartment();
                frmDepartment.value = department;
                frmDepartment.Visible = true;
                frmDepartment.Show();
            }
        }
       
    }
}
